using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BerkoRules.Entities;
using BerkoRules;
using Microsoft.VisualBasic.CompilerServices;
using BerkoRules.Mappers;

namespace Berko
{
    public partial class frmNeumaticos : Form
    {
        private bool _principal;
        private NeumaticosList _neumaticos;

        public bool Desde_Principal
        {
            get
            {
                return this._principal;
            }
            set
            {
                this._principal = value;
            }
        }


        public frmNeumaticos()
        {
            InitializeComponent();
            this._neumaticos = null;
            this._principal = false;
        }

        private void cmdEliminar1_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("\x00bfDesea eliminar el neumatico seleccionado?", "Eliminar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) && this.Eliminar())
            {
                if (this._principal)
                {
                    this.Traer_neumaticos();
                    this.txtNeumaticos.Text = string.Empty;
                    this.txtNeumaticos.Focus();
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void cmdModif1_Click(object sender, EventArgs e)
        {
            if (this.Validar_Campo() && this.Modificar())
            {
                if (this._principal)
                {
                    this.Traer_neumaticos();
                    this.txtNeumaticos.Text = string.Empty;
                    this.txtNeumaticos.Focus();
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void cmdNuevo1_Click(object sender, EventArgs e)
        {
            if (this.Validar_Campo() && this.Nuevo())
            {
                if (this._principal)
                {
                    this.Traer_neumaticos();
                    this.txtNeumaticos.Text = string.Empty;
                    this.txtNeumaticos.Focus();
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void cmdSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool Eliminar()
        {
            try
            {
                NeumaticosMapper.Instance().Delete(this._neumaticos[this.cboNeumaticos.SelectedIndex].neu_ID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo eliminar el neumatico seleccionado. Verifique que no est\x00e9 siendo utilizado", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return true;
        }

        private void frmActividad_Load(object sender, EventArgs e)
        {
            if ((this.Traer_neumaticos() && (this._neumaticos != null)) && (this._neumaticos.Count > 0))
            {
                this.cboNeumaticos.SelectedIndex = 0;
                this.txtNeumaticos.Text = this.cboNeumaticos.Text;
                this.cmdModif1.Enabled = true;
                this.cmdEliminar1.Enabled = true;
            }
            this.txtNeumaticos.Focus();
        }

        private bool Modificar()
        {
            this._neumaticos[this.cboNeumaticos.SelectedIndex].neu_Nombre = this.txtNeumaticos.Text.Trim();
            try
            {
                NeumaticosMapper.Instance().Save(this._neumaticos[this.cboNeumaticos.SelectedIndex]);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo actualizar el neumatico seleccionado. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return true;
        }

        private bool Nuevo()
        {
            Neumaticos row = new Neumaticos();
            row.neu_Nombre = this.txtNeumaticos.Text.Trim();
            try
            {
                NeumaticosMapper.Instance().Insert(row);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo guardar el motor. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return true;
        }

        private bool Traer_neumaticos()
        {
            try
            {
                this._neumaticos = NeumaticosMapper.Instance().GetAll();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los neumaticos. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            this.cboNeumaticos.DataSource = this._neumaticos;
            this.cboNeumaticos.DisplayMember = "neu_Nombre";
            this.cboNeumaticos.ValueMember = "neu_ID";
            return true;
        }

        private void txtNeumaticos_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)(e.KeyChar) == 13)
            {
                this.cmdNuevo1.Focus();
            }
        }

        private bool Validar_Campo()
        {
            if (this.txtNeumaticos.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe ingresar un valor v\x00e1lido", "Campo vac\x00edo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtNeumaticos.Focus();
                return false;
            }
            return true;
        }
    }
}